package com.lbs.jiepiaisi.config;

/**
 * Created by yu on 2020-06-11 18:24.
 * Description: 通过此类获取OkHttp下载的二进制数据
 */

import android.os.Handler;
import android.os.Looper;

import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

import java.io.FileInputStream;
import java.io.IOException;

import okhttp3.MediaType;
import okhttp3.MultipartBody;
import okhttp3.RequestBody;
import okio.Buffer;
import okio.BufferedSink;
import okio.ForwardingSink;
import okio.Okio;


public class ProgressRequestBody extends RequestBody {

    private UploadCallbacks mListener;
    private MultipartBody mMultipartBody;
    private long mCurrentLength;

    public interface UploadCallbacks {

        void onProgress(long totalLen, long writtenLen);

        //void onError();
    }

    public ProgressRequestBody(MultipartBody multipartBody, UploadCallbacks listener) {
        mMultipartBody = multipartBody;
        mListener = listener;
    }

    @Override
    public long contentLength() throws IOException {
        return mMultipartBody.contentLength();
    }

    @Nullable
    @Override
    public MediaType contentType() {
        return mMultipartBody.contentType();
    }

    @Override
    public void writeTo(@NotNull BufferedSink bufferedSink) throws IOException {
//        long totalLength = contentLength();
//        byte[] buffer = new byte[1024];
////        FileInputStream in = new FileInputStream(mFile);
//        long writenLength = 0;
//        try {
//            int read;
//            Handler handler = new Handler(Looper.getMainLooper());
//            while ((read = in.read(buffer)) != -1) {
//                // update progress on UI thread
//                handler.post(new ProgressUpdater(writenLength, totalLength));
//                writenLength += read;
//                bufferedSink.write(buffer, 0, read);
//
//            }
//        } finally {
//            in.close();
//        }

        //这里需要另一个代理类来获取写入的长度
        ForwardingSink forwardingSink = new ForwardingSink(bufferedSink) {
            final long totalLength = contentLength();
            @Override
            public void write(Buffer source, long byteCount) throws IOException {
                //写入的长度
                mCurrentLength += byteCount;
                //回调进度
                if(mListener != null) {
                    mListener.onProgress(totalLength, mCurrentLength);
                }
                super.write(source, byteCount);
            }
        };

        BufferedSink mSink = Okio.buffer(forwardingSink);
        //写数据
        mMultipartBody.writeTo(mSink);
        //刷新数据
        mSink.flush();
    }

    private class ProgressUpdater implements Runnable {
        private long mWrittenLen;
        private long mTotalLen;

        public ProgressUpdater(long writtenLen, long totalLen) {
            mWrittenLen = writtenLen;
            mTotalLen = totalLen;
        }

        @Override
        public void run() {
            mListener.onProgress(mTotalLen, mWrittenLen);
        }
    }
}

